package com.cloudbroker.bcs.platform.ltb.middle.bizservice.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cloudbroker.bcs.common.util.VOUtil;
import com.cloudbroker.bcs.platform.ltb.bizapi.constants.HSBCPSErrorCodes;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.admin.AdminPayBizService;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.admin.back.OrderListQryBack;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.admin.back.OrderListQryData;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.admin.vo.OrderListQryForm;
import com.cloudbroker.bcs.platform.ltb.common.payUtil.HttpUtils;
import com.cloudbroker.bcs.platform.ltb.common.payUtil.bean.OrderRequest;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by daibd19671 on 2016/11/8.
 */
@Service
public class AdminPayServiceImpl implements AdminPayBizService{


    @Override
    public OrderListQryBack orderListQry(OrderListQryForm form) {
        OrderRequest orderRequest = new OrderRequest();
        orderRequest.setPartner_id("1262260201");
        orderRequest.setPartner_serial_no("20150100813493400000001");
        orderRequest.setBegin_time(form.getBegin_time());
        orderRequest.setEnd_time(form.getEnd_time());
        orderRequest.setOrder_status(form.getOrder_status());
        orderRequest.setSendercomp_id("1262260201");
        orderRequest.setTargetcomp_id("91000");
        String resp = null;
        try {
            resp = HttpUtils.proceed(orderRequest, HttpUtils.epay_query_orders);
        } catch (Exception e) {
            e.printStackTrace();
            return VOUtil.genErrorResult(HSBCPSErrorCodes.CombinationErrorInfo.ORDER_LIST_QRY_ERR,OrderListQryBack.class);
        }
        JSONObject respObj = JSONObject.parseObject(resp);
        if(respObj.get("error_no")!=null && !respObj.get("error_no").equals("0")){
            return VOUtil.genErrorResult(HSBCPSErrorCodes.CombinationErrorInfo.ORDER_LIST_QRY_ERR,OrderListQryBack.class);
        }
        OrderListQryBack orderListQryBack = new OrderListQryBack();
        List<OrderListQryData> dataList = new ArrayList<>();
        String[] strs = resp.split("\n");
        for(int i =0;i<strs.length;i++){
            String str = strs[i];
            if (StringUtils.isEmpty(str.trim())) {
                continue;
            }
            if (i == 0) {
                String[] msgs = str.split("|");
                orderListQryBack.setPartner_id(msgs[0]);
                orderListQryBack.setTotal_num(msgs[1]);
                orderListQryBack.setTotal_amount(msgs[2]);
            } else if (i == (strs.length - 1)) {
                orderListQryBack.setSigh(str);
            } else {
                String[] msgs = str.split("\\|");
                OrderListQryData orderListQryData = new OrderListQryData();
                orderListQryData.setPartner_id(msgs[0]);
                orderListQryData.setCheck_date(msgs[1]);
                orderListQryData.setPay_date(msgs[2]);
                orderListQryData.setPartner_serial_no(msgs[3]);
                orderListQryData.setYunpay_order_id(msgs[4]);
                orderListQryData.setOccur_balance(msgs[5]);
                orderListQryData.setPoundage(msgs[6]);
                orderListQryData.setOrder_type(msgs[7]);
                orderListQryData.setOrder_status(msgs[8]);
                dataList.add(orderListQryData);
            }
        }
        String jsonStr = JSON.toJSONString(dataList);
        orderListQryBack.setData_str(jsonStr);
        return VOUtil.setSuccessResult(orderListQryBack);
    }

    @Override
    public OrderListQryBack test_orderListQry(OrderListQryForm form) {
        String resp = "30000001|9|210002.00|\n" +
                "30000001|20150415|20150415145425|P2015041510255486040627|20150415145425103559643|30000.00|0.00|G|3|\n" +
                "30000001|20150415|20150415145609|P2015041510255486040628|20150415145609103559647|30000.00|0.00|G|3|\n" +
                "30000001|20150415|20150415150250|P2015041510255486040629|20150415150250103559653|30000.00|0.00|G|3|\n" +
                "30000001|20150415|20150415150737|P2015041510255486040630|20150415150737103559659|30000.00|0.00|G|7|\n" +
                "30000001|20150415|20150415151233|P2015041510255486040632|20150415151233103559663|30000.00|0.00|G|7|\n" +
                "30000001|20150415|20150415151340|P2015041510255486040633|20150415151340103559665|30000.00|0.00|D|7|\n" +
                "30000001|20150415|20150415152334|P2015041510255486040635|20150415152334103559671|30000.00|0.00|R|2|\n" +
                "30000001|20150415|20150415191619|2015041517490000|20150415191620103560017|1.00|0.00|D|2|\n" +
                "30000001|20150415|20150415193723|1429097843650|20150415193726103560505|1.00|0.00|R|2|\n" +
                " \n" +
                "UuWMqfqDo7h5UQJCD5XrL0YRI7xearxOnP6VThyh6...5Tqz9pcrn7bGVb5tt1uWu8yGS6WzuA=";
        OrderListQryBack orderListQryBack = new OrderListQryBack();
        List<OrderListQryData> dataList = new ArrayList<>();
        String[] strs = resp.split("\n");
        for(int i =0;i<strs.length;i++){
            String str = strs[i];
            if (StringUtils.isEmpty(str.trim())) {
                continue;
            }
            if (i == 0) {
                String[] msgs = str.split("|");
                orderListQryBack.setPartner_id(msgs[0]);
                orderListQryBack.setTotal_num(msgs[1]);
                orderListQryBack.setTotal_amount(msgs[2]);
            } else if (i == (strs.length - 1)) {
                orderListQryBack.setSigh(str);
            } else {
                String[] msgs = str.split("\\|");
                OrderListQryData orderListQryData = new OrderListQryData();
                orderListQryData.setPartner_id(msgs[0]);
                orderListQryData.setCheck_date(msgs[1]);
                orderListQryData.setPay_date(msgs[2]);
                orderListQryData.setPartner_serial_no(msgs[3]);
                orderListQryData.setYunpay_order_id(msgs[4]);
                orderListQryData.setOccur_balance(msgs[5]);
                orderListQryData.setPoundage(msgs[6]);
                orderListQryData.setOrder_type(msgs[7]);
                orderListQryData.setOrder_status(msgs[8]);
                dataList.add(orderListQryData);
            }
        }
        String jsonStr = JSON.toJSONString(dataList);
        orderListQryBack.setData_str(jsonStr);
        return VOUtil.setSuccessResult(orderListQryBack);
    }

//    public static void main(String[] args) {
//        String resp = "30000001|9|210002.00|\n" +
//                "30000001|20150415|20150415145425|P2015041510255486040627|20150415145425103559643|30000.00|0.00|G|3|\n" +
//                "30000001|20150415|20150415145609|P2015041510255486040628|20150415145609103559647|30000.00|0.00|G|3|\n" +
//                "30000001|20150415|20150415150250|P2015041510255486040629|20150415150250103559653|30000.00|0.00|G|3|\n" +
//                "30000001|20150415|20150415150737|P2015041510255486040630|20150415150737103559659|30000.00|0.00|G|7|\n" +
//                "30000001|20150415|20150415151233|P2015041510255486040632|20150415151233103559663|30000.00|0.00|G|7|\n" +
//                "30000001|20150415|20150415151340|P2015041510255486040633|20150415151340103559665|30000.00|0.00|D|7|\n" +
//                "30000001|20150415|20150415152334|P2015041510255486040635|20150415152334103559671|30000.00|0.00|R|2|\n" +
//                "30000001|20150415|20150415191619|2015041517490000|20150415191620103560017|1.00|0.00|D|2|\n" +
//                "30000001|20150415|20150415193723|1429097843650|20150415193726103560505|1.00|0.00|R|2|\n" +
//                " \n" +
//                "UuWMqfqDo7h5UQJCD5XrL0YRI7xearxOnP6VThyh6...5Tqz9pcrn7bGVb5tt1uWu8yGS6WzuA=";
//        OrderListQryBack orderListQryBack = new OrderListQryBack();
//        List<OrderListQryData> dataList = new ArrayList<>();
//        String[] strs = resp.split("\n");
//        for(int i =0;i<strs.length;i++){
//            String str = strs[i];
//            if(StringUtils.isEmpty(str.trim())){
//                continue;
//            }
//            if(i == 0){
//                String[] msgs = str.split("|");
//                orderListQryBack.setPartner_id(msgs[0]);
//                orderListQryBack.setTotal_num(msgs[1]);
//                orderListQryBack.setTotal_amount(msgs[2]);
//            }else if(i==(strs.length-1)){
//                orderListQryBack.setSigh(str);
//            }else{
//                String[] msgs = str.split("\\|");
//                OrderListQryData orderListQryData = new OrderListQryData();
//                orderListQryData.setPartner_id(msgs[0]);
//                orderListQryData.setCheck_date(msgs[1]);
//                orderListQryData.setPay_date(msgs[2]);
//                orderListQryData.setPartner_serial_no(msgs[3]);
//                orderListQryData.setYunpay_order_id(msgs[4]);
//                orderListQryData.setOccur_balance(msgs[5]);
//                orderListQryData.setPoundage(msgs[6]);
//                orderListQryData.setOrder_type(msgs[7]);
//                orderListQryData.setOrder_status(msgs[8]);
//                dataList.add(orderListQryData);
//            }
//        }
//        String jsonStr = JSON.toJSONString(dataList);
//        orderListQryBack.setData_str(jsonStr);
//    }
}
